package com.asset.salesforce.validator;

import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;

import com.asset.salesforce.domain.StockSku;
import com.asset.salesforce.domain.StockSubCategory;
import com.asset.salesforce.form.StockSkuForm;
import com.asset.salesforce.form.StockSubCategoryForm;
import com.asset.salesforce.service.InventoryService;

public class SkuCodeNotExistConstraintValidator implements ConstraintValidator<SkuCodeNotExist, Object>{
	@Autowired
	private InventoryService inventoryService;
	@Override
	public void initialize(SkuCodeNotExist arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public boolean isValid(Object arg0, ConstraintValidatorContext arg1) {
		StockSkuForm stockSkuForm = (StockSkuForm)arg0;
		boolean success = false; 
		List<StockSku> stockSkus = inventoryService.getAllSKUsByEmpId(stockSkuForm.getEmpId(), stockSkuForm.getParentId());
		if(stockSkus.size()==0){
			success=true;
		}
		else{
		for (StockSku stockSku : stockSkus) {
			if(!stockSku.getCode().equals(stockSkuForm.getCode())){
				success=true;
			}else{
				success=false;
			}
		}
		}
		return success;
	   }
}
